<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-show="loading" class="article-loading" />
    <!-- /加载中 loading -->

    <!-- 404错误提示 -->
    <div class="error" v-if="is404">
      您要找的文章走丢了.....
    </div>
    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image :src="article.aut_photo" round width="1rem" height="1rem" fit="fill" />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | relativeTime}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="hSwitchFollow"
        >{{article.is_followed ? '取关' : '+ 关注'}}</van-button>
      </div>

      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button round size="small" hairline type="primary" plain icon="good-job-o">点赞</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round size="small" hairline type="danger" plain icon="delete">不喜欢</van-button>
      </div>
    </div>
    <!-- /文章详情 -->

    <!-- 文章评论 -->
    <article-comment :articleId="$route.params.id"></article-comment>
    <!-- 文章评论 -->
  </div>
</template>

<script>
import { followUser, unFollowUser } from '@/api/user'
import { getArticle } from '@/api/article'
import ArticleComment from './comment'
export default {
  name: 'ArticleIndex',
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: { },
      is404: false
    }
  },
  components: {
    ArticleComment
  },
  beforeRouteLeave (to, from, next) {
    console.log('你现在在文章详情页，你将要离开这个页面')
    console.log(to, from)
    if (to.path === '/login' && this.$store.state.tokenInfo.token) {
      next({ path: '/' })
    } else {
      next()
    }
  },
  created () {
    this.loadAritcle()
  },
  methods: {
    async loadAritcle () {
      try {
        const result = await getArticle(this.$route.params.id)
        console.log(result)
        // 保存文章详情
        this.article = result.data.data
        // 取消加载状态
        this.loading = false
      } catch (err) {
        // console.dir(err)
        // 取消加载状态
        this.loading = false
        if (err && err.response && err.response.status === 404) {
          // 确定文章没有找到
          this.is404 = true
        } else {
          this.$toast.fail('文章加载失败')
        }
      }
    },
    async hSwitchFollow () {
      try {
        // 根据当前is_followed的值来决定调用什么方法
        if (this.article.is_followed) {
          // 目前是ture,点击按钮 是要做取消关注
          const result = await unFollowUser(this.article.aut_id)
          console.log(result)
        } else {
          const result = await followUser(this.article.aut_id)
          console.log(result)
        }
        // 1. 更新视图
        //  方式一：再发一次获取文章详情的请求。 这个成本高。
        //  方式二：只需修改本地数据。如果上面的操作没有报错，则说明后端接口工作是正常的。
        this.article.is_followed = !this.article.is_followed

        // 2. 给一个提示
        this.$toast.success('操作成功')
      } catch (e) {
        console.log(e)
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
